package task

import (
	"encoding/json"
	"github.com/robfig/cron"
	"log"
	"qmp_go_push/models"
)

type JobConfig struct {
	Spec string
	Cmd  cron.Job
}

var jobList = []JobConfig{}

func init() {
	jobList = append(jobList, JobConfig{Spec: "@hourly", Cmd: models.TestJob{JobName: "测试定时任务"}})
}

func InitCronJob() {
	c := cron.New()
	for _, job := range jobList {
		if err := c.AddJob(job.Spec, job.Cmd); err != nil {
			log.Fatal("Fatal error c.AddJob(): \n", err.Error())
		}
	}
	// 启动
	c.Start()
	log.Println("===============初始化CronJob启动成功===============")
	marshalIndent, _ := json.MarshalIndent(jobList, "", "\t")
	log.Println(string(marshalIndent))
	log.Println()
	log.Println()
}
